/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2007 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Contains code common to all charts.
 *
 * @author Julien Chastang
 */
abstract class AbstractGChart implements GChart {
		
	private static final String TITLE_WITH_COLOR_AND_SIZE_PARAM = "chts=";
	private static final String CHART_FILL_PARAM                = "chf=";
	private static final String LEGEND_PARAM                    = "chdl=";
	private static final String TITLE_PARAM                     = "chtt=";
	private static final String COLOR_PARAM                     = "chco=";
	private static final String SIZE_PARAM                      = "chs=";
	private static final String DATA_PARAM                      = "chd=e:";

	private final Map<String,String> parameterMap = new HashMap<String, String>();
	
	protected static final String GCHART_URL = "http://chart.apis.google.com/chart";

	/**
	 * 
	 */
	AbstractGChart(){
		addParameter(SIZE_PARAM,"200x125"); //Default chart size.
	}
    
    /**
     * 
     * @param data
     * @param dataSeriesSperator
     */
    protected final void setData(final List<Data> data){
		String dataParam = "";
		int count = 0;
		for (Data datum : data) {
			if (datum == null) continue;
			dataParam += (count++ == 0) ? datum : "," + datum;
		}
		if (count > 0){
			addParameter(DATA_PARAM, dataParam);
		}
    }

	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#setSize(int, int)
	 */
	public void setSize(final int x, final int y){
		addParameter(SIZE_PARAM, x + "x" + y);
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#createURLString()
	 */
	public final String createURLString() {
		prepareData();
		String url = getBaseURLString();
		String params = "";
		for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
			params += "&" + entry.getKey() + "" + entry.getValue();
		}
		return url + params;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#createURLForHTML()
	 */
	public final String createURLForHTML() {
		return createURLString().replaceAll("&", "&amp;");
	}
	
	/**
	 * Internal method to set chart colors.
	 * @param colors
	 */
	protected final void setColors(final List<Color> colors) {
		String colorParam = "";
		int count = 0;
		for (Color color : colors) {
			if (color == null) continue;
			colorParam += (count++ == 0) ? color : "," + color;
		}
		if (count > 0){
			addParameter(COLOR_PARAM, colorParam);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#setTitle(java.lang.String)
	 */
	public void setTitle(final String title){
		addParameter(TITLE_PARAM, title.replaceAll("\\s", "+"));
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#setTtile(java.lang.String, com.googlecode.gchartjava.Color, int)
	 */
	public void setTitle(final String title, Color color, int fontSize){
		setTitle(title);
		addParameter(TITLE_WITH_COLOR_AND_SIZE_PARAM, color + "," + fontSize);
	}
	
	/**
	 * Internal method to set chart legends.
	 * @param legends
	 */
	protected final void setLegends(final List<String> legends){
		String legendParam = "";
		int count = 0;
		for (String legend : legends) {
			if (legend == null) continue;
			final String l = legend.replaceAll("\\s", "+");
			legendParam += (count++ == 0) ? l : "|" + l;
		}
		if(count > 0)
		  addParameter(LEGEND_PARAM, legendParam);
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#setAreaFill(com.googlecode.gchartjava.Fill)
	 */
	public void setAreaFill(final Fill fill){
		String value = parameterMap.get(CHART_FILL_PARAM);
		if (value == null){
			addParameter(CHART_FILL_PARAM, "c," + fill);
		}
		else{
			value += "|" +  "c," + fill;
			addParameter(CHART_FILL_PARAM, value);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.gchartjava.GChart#setBackgroundFill(com.googlecode.gchartjava.Fill)
	 */
	public void setBackgroundFill(final Fill fill){
		String value = parameterMap.get(CHART_FILL_PARAM);
		if (value == null){
			addParameter(CHART_FILL_PARAM, "bg," + fill);
		}
		else{
			value += "|" +  "bg," + fill;
			addParameter(CHART_FILL_PARAM, value);
		}
	}
	
	/**
	 * Convenience method for adding parameters to request.
	 * @param key
	 * @param value
	 */
	protected final void addParameter(final String key, final String value){
		  parameterMap.put(key, value); 
	}
	
	/**
	 * Prepare data for URL String formation.
	 */
	protected abstract void prepareData();
	
	/**
	 * Return the base URL String for the chart type.
	 * @return
	 */
	protected abstract String getBaseURLString();
}